package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.MessageCenterAdapter;
import com.niji.littly_donkey_android.adapter.NoticeAdapter;
import com.niji.littly_donkey_android.bean.Message;
import com.niji.littly_donkey_android.bean.Notice;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NoticeActivity extends BaseActivity {

    private Button notice_return;

    private ListView notice_list;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notice_activity);
        init();
        noticeReturn();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.notice_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }
    private void init() {
        showNoticeList();
    }

    private void showNoticeList() {
        notice_list = findViewById(R.id.notice_list);
        ArrayList<Notice> notices = new ArrayList<>();
        String url = UrlUtil.URL + "/user/comments/log/list?page=1&per_page=50";
        RequestQueue requestQueue= Volley.newRequestQueue(NoticeActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map  = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0){
                        Map<String, Object>  dataMap = (Map<String, Object>) map.get("data");
                        List<Map<String,Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        for (int i = 0; i < mapList.size(); i++) {
                            Integer id = (Integer) mapList.get(i).get("id");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer comments_id = (Integer) mapList.get(i).get("comments_id");
                            Integer type = (Integer) mapList.get(i).get("type");
                            Integer status = (Integer) mapList.get(i).get("status");
                            String created_at = (String) mapList.get(i).get("created_at");
                            Map<String,Object> userInfo = (Map<String, Object>) mapList.get(i).get("userInfo");
                            String nickname = (String) userInfo.get("nickname");
                            String avatar = (String) userInfo.get("avatar");
                            Notice notice = new Notice();
                            if (type == 1){
                                notice.setTitle("#" + "点赞");
                                notice.setMessage(nickname + "点赞了你");
                            }else if(type == 2){
                                notice.setTitle("#" + "评论");
                                notice.setMessage(nickname + "评论了你");

                            }else if(type == 3){
                                notice.setTitle("#" + "点赞视频");
                                notice.setMessage(nickname + "点赞了你的视频");
                            }
                            notice.setTime(created_at);
                            notices.add(notice);
                        }
                        notice_list.setAdapter(new NoticeAdapter(NoticeActivity.this,notices));
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(NoticeActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("错误", volleyError.toString());
                Toast.makeText(NoticeActivity.this, "网络失败", Toast.LENGTH_SHORT).show();
            }
        }){


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(NoticeActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }


    private void noticeReturn() {
        notice_return = findViewById(R.id.notice_return);
        notice_return = findViewById(R.id.notice_return);
        notice_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
